package com.shy.mq.service;

import cn.hutool.json.JSONUtil;
import com.shy.mq.MessageNotice;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RabbitMessageService {

    @Value("${exchangeNotice}")
    private String exchangeNoticeName;

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendNoticeMessage(MessageNotice messageNotice){
        //延迟队列
        rabbitTemplate.convertAndSend(exchangeNoticeName,"kq.notice",messageNotice,message -> {
                    // 设置消息header，增加x-delay , 毫秒单位
                    message.getMessageProperties().setHeader("x-delay",300);
                    return message;
                });
        System.out.println("发送消息的日志："+ JSONUtil.toJsonStr(messageNotice));
    }
}